/*-
 * IncomingAtParser.java
 *
 * Copyright (c) 2007, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention.at;

import static com.googlecode.attention.ComLogStatements.*;

/**
 * Parser that parses incoming AT commands.
 *
 * @author Stein Gunnar Bakkeby
 */
public final class IncomingAtParser extends AtCommand<IncomingAtParser> {
  
  private static IncomingAtParser iap;
  private IncomingAtParser() {
    super(null);
    setCommand("INCOMING", Command.READ);
  }
  
  public IncomingAtParser(final AtListener<IncomingAtParser> listener) {
    super(listener);
  }
  
  /**
   * Returns a singleton instance of this parser. The use of the singleton or
   * the constructor is optional.
   * 
   * @return Singleton instance of IncomingAtParser.
   */
  public static IncomingAtParser getInstance() {
    if (iap == null) iap = new IncomingAtParser();
    return iap;
  }

//  /**  TODO re-examine this!
//   * This will add an AT listener to this parser.
//   * 
//   * @param at The incoming AT to listen for.
//   * @param listener The listener to add.
//   */
//  public void addListener(final IncomingAt at, AtListener<AtCommand> listener){
//    at.getAtCommand().setListener(listener);
//  }
//  
//  /**
//   * This will clear all listeners for a given incoming AT.
//   * 
//   * @param at The incoming AT to remove all listeners from.
//   */
//  public void clearAllListeners(final IncomingAt at) {
//    at.getAtCommand().clearAllListeners();    
//  }
//  
//  /**
//   * This will clear all listeners for all incoming AT commands.
//   */
//  @Override
//  public void clearAllListeners() {
//    for (IncomingAt at : IncomingAt.values()) {
//      at.getAtCommand().clearAllListeners();
//    }
//  }
  
  /**
   * This will try to parse the response and identify which incoming AT command
   * the response belongs to.
   * 
   * @param response The response/signal from the handset.
   */
  @Override
  public void parse(final String response) {
    COM_DEBUG.log("Identifying incoming AT command: " + response);
    if (response.length() > 0) {
      /* Receiving a value 0xFFFFFFFF when connecting
       * or disconnecting the phone. */
      if (response.charAt(0) == 0xFFFFFFFF) {
        IncomingAt.CONNECTION_RESET.getAtCommand().parse(response);
      }
      if (response.charAt(0) == 0x0) {
        IncomingAt.CONNECTION_RESET.getAtCommand().parse(response);
      }
      
      /* Receiving a string "RING" when the phone is ringing. */
      else if (response.trim().equalsIgnoreCase("RING")) {
        IncomingAt.RING.getAtCommand().parse(response);
      }
      
      /* If not recognised then discard content */
      else {
        COM_006.log(response);
      }
    }
  }

  @Override
  protected IncomingAtParser getThis() {
    return this;
  }
}